package com.fufu.mq;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 消费者
 */
public class Consumer {

    public static void main(String[] args) throws Exception {

        String exchangeName = "my-first-exchange";
        String queueName = "my-first-queue";

        // 创建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 服务地址
        factory.setHost("localhost");
        // 端口号
        factory.setPort(5672);
        // 用户名
        factory.setUsername("guest");
        // 密码
        factory.setPassword("guest");

        // 新建连接
        Connection connection = factory.newConnection();
        // 创建信道
        Channel channel = connection.createChannel();

        // 接收消息回调
        DeliverCallback deliverCallback = (s, delivery) -> System.out.println("接收到消息："+ new String(delivery.getBody()) );
        // 中断消息回调
        CancelCallback cancelCallback = (consumerTag) -> System.out.println("消费消息被终端");

        // 消费消息
        channel.basicConsume(queueName,true, deliverCallback, cancelCallback);
    }
}
